<?php

    /**
    * This is the model class for table "customer".
    *
    * The followings are the available columns in table 'customer':
    * @property string $id
    * @property string $phone
    * @property string $password
    * @property string $name
    * @property string $address
    * @property integer $point
    * @property integer $gender
    * @property integer $dob
    * @property integer $is_receive_sms
    * @property integer $bill_count
    *
    * The followings are the available model relations:
    * @property Bill[] $bills
    */
    class Customer extends Model
    {
        /**
        * Returns the static model of the specified AR class.
        * @param string $className active record class name.
        * @return Customer the static model class
        */
        public static function model($className=__CLASS__)
        {
            return parent::model($className);
        }

        /**
        * @return string the associated database table name
        */
        public function tableName()
        {
            return 'customer';
        }

        /**
        * @return array validation rules for model attributes.
        */
        public function rules()
        {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                array('phone', 'required'),
                array('name', 'required', 'on' => 'update'),
                array('point', 'numerical', 'integerOnly'=>true),
                array('phone, is_receive_sms', 'length', 'max'=>11),
                array('name, gender, dob', 'length', 'max'=>50),
                array('address, password, created, reset_time, status, bill_count', 'length', 'max'=>255),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id, phone, name, address, point', 'safe', 'on'=>'search'),
            );
        }

        /**
        * @return array relational rules.
        */
        public function relations()
        {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
                'bills' => array(self::HAS_MANY, 'Bill', 'customer_id'),
            );
        }

        /**
        * @return array customized attribute labels (name=>label)
        */
        public function attributeLabels()
        {
            return array(
                'id' => 'ID',
                'phone' => 'Số điện thoại',
                'name' => 'Tên đầy đủ',
                'address' => 'Địa chỉ',
                'point' => 'Điểm thưởng',
                'gender' => 'Giới tính',
                'dob' => 'Ngày tháng năm sinh',
                'is_receive_sms' => 'Nhận tin nhắn sms',
                'bill_count' => 'Tổng đơn hàng',
            );
        }
        
        public function getHashPassword($password){
            Yii::import('ext.PasswordHash');
            $hasher = new PasswordHash(8, FALSE);
            $hash = $hasher->HashPassword($password);
            return $hash;
        }

        public function checkPassword($password, $hashPassword){
            $newPass = $this->getHashPassword($password);
            Yii::import('ext.PasswordHash');
            $hasher = new PasswordHash(8, FALSE);
            $check = $hasher->CheckPassword($password, $hashPassword);
            return $check;
        }

        public function getStatusData() {
            return array(
                'ENABLE' => 'Hoạt động',
                'DISABLE' => 'Khóa',
            );
        }
        public function getStatusLabel() {
            return $this->statusData[$this->status];
        }
        public function getIsDisable(){
            return in_array($this->status, array('DISABLE'));
        }
        
        
        public function getNewPassword(){
            return rand(100000, 999999);
        }
        
        public function getNameAlias(){
            $nameAlias = TextParser::toSEOString($this->name, ' ');
            return ucwords(strtolower($nameAlias));
        }
        
        public function getNameDetect(){
            return $this->name ? $this->name : $this->phone;
        }
        

        public function getGenderData(){
            return array(
                'MALE' => 'Nam',
                'FEMALE' => 'Nữ',
            );
        }

        public function getGenderLabel(){
            return $this->gender ? $this->genderData[$this->gender] : NULL;
        }
        
        public function getAdminUrl($type = 'view') {
            return Yii::app()->createUrl('/admin/customer/' . $type, array("id" => $this->id));
        }

}